﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public partial class DrugInForm : BaseForm
    {
        public DrugInForm()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LingyaoForm form = new LingyaoForm();
            if (DialogResult.OK == form.ShowDialog())
            {
                try
                {
                    AnesthesiaBLL.AddLingyaon(form.linyao);
                    FillLingyaoList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("领药登记失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void RefreshForm()
        {
            FillLingyaoList();
            FillDrugList();
        }

        private void DrugInForm_Load(object sender, EventArgs e)
        {
            FillLingyaoList();
            FillDrugList();
        }

        private void FillDrugList()
        {
            List<Yaopin> yaopins = AnesthesiaBLL.GetAllYaopin();
            Yaopin p = new Yaopin();
            p.YaopinID = "";
            p.YaopinName = "全部药品";
            yaopins.Insert(0, p);
            
            toolStripComboBoxDrug.ComboBox.DataSource = yaopins;
            toolStripComboBoxDrug.ComboBox.DisplayMember = "YaopinLongName";
            toolStripComboBoxDrug.ComboBox.ValueMember = "YaopinID";
            
        }

        private void FillLingyaoList(List<Lingyao> lingyaos)
        {
            listView1.Items.Clear();
            foreach (Lingyao y in lingyaos)
            {
                ListViewItem lvit = new ListViewItem(y.LingyaoYaopin.YaopinCode);
                lvit.Tag = y;

                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoYaopin.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoYaopin.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoYaopin.YaopinDanwei;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoNumber.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoDate.ToShortDateString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoCode;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.LingyaoComment;
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void FillLingyaoList()
        {
            List<Lingyao> lingyaos = AnesthesiaBLL.GetAllLingyao();
            FillLingyaoList(lingyaos);
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择领药记录！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (listView1.SelectedItems.Count > 1)
            {
                MessageBox.Show("一次只能修改一项领药记录！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            Lingyao y = listView1.SelectedItems[0].Tag as Lingyao;
            LingyaoForm form = new LingyaoForm();
            form.linyao = y;
            if (DialogResult.OK == form.ShowDialog())
            {
                try
                {
                    AnesthesiaBLL.ModifyLingyao(form.linyao);
                    FillLingyaoList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改领药记录失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButtonDel_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择领药记录！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (MessageBox.Show("领药记录删除后将不能回复。是否确定要删除？", "医院", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    for (int i = 0; i < listView1.SelectedItems.Count; i++)
                    {
                        Lingyao y = listView1.SelectedItems[i].Tag as Lingyao;
                        AnesthesiaBLL.DeleteLingyao(y);
                    }

                    FillLingyaoList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除领药记录失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void toolStripComboBoxDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Yaopin y = new Yaopin();
                y.YaopinID = toolStripComboBoxDrug.ComboBox.SelectedValue.ToString();
                List<Lingyao> lys = new List<Lingyao>();
                if (y.YaopinID.Length > 0)
                    lys = AnesthesiaBLL.GetAllLingyaoByYaopin(y);
                else
                    lys = AnesthesiaBLL.GetAllLingyao();
                FillLingyaoList(lys);
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除领药记录失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
